package com.mengniu.commerce.collect.service.impl.item;


import com.mengniu.commerce.collect.service.WmItemService;
import com.mengniu.commerce.collect.third.request.IWeiMengGoodsRequest;
import com.mengniu.commerce.collect.third.response.item.GoodsInfoModel;
import com.mengniu.commerce.collect.third.response.item.GoodsListResponse;
import com.mengniu.commerce.collect.util.RestTemplateUtil;
import com.mengniu.commerce.common.core.utils.StringUtils;
import com.mengniu.commerce.sync.api.RemoteItemService;
import com.mengniu.commerce.sync.api.domain.BaseMnItemModel;
import com.mengniu.commerce.sync.api.domain.CreateItemModel;
import com.mengniu.commerce.sync.api.domain.ModifyItemModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 商品管理 服务层实现
 *
 * @author yz_sm
 */
@Service
public class WmItemServiceImpl implements WmItemService
{

    @Autowired
    private RestTemplateUtil restTemplateUtil;

    @Resource
    private RemoteItemService remoteItemService;

    @Autowired
    private IWeiMengGoodsRequest weiMengGoodsRequest;

    @Override
    public void createItemByMessage(Long bosId,Long goodsId) {
        List<CreateItemModel> list = new ArrayList<>();
        //查询商品
        String token = restTemplateUtil.getToken(bosId);
        //通过id查询商品详情进行参数封装
        GoodsInfoModel goodsInfo = weiMengGoodsRequest.getGoodsInfoById(goodsId, token);
        CreateItemModel createItemModel = new  CreateItemModel();
        list.add(this.convert(goodsInfo ,createItemModel));
        remoteItemService.createItem(list, "youzan-collect");
    }

    /***
    * @Description: 通过微盟消息修改商品
    * @Param: []
    * @return: void
    * @Author: zfw
    * @Date: 2023/12/3
    * @Date: 12:07
    */
    @Override
    public void updateItemByMessage(Long bosId,Long goodsId) {
        ModifyItemModel modifyItemModel = new ModifyItemModel();
        //查询商品
        String token = restTemplateUtil.getToken(bosId);
        //通过id查询商品详情进行参数封装
        GoodsInfoModel goodsInfo = weiMengGoodsRequest.getGoodsInfoById(goodsId, token);
        modifyItemModel = this.convertUpdate(goodsInfo,modifyItemModel);
        remoteItemService.modify(modifyItemModel, "youzan-collect");
    }

    /***
    * @Description: 存量数据创建到蒙牛
    * @Param: []
    * @return: void
    * @Author: zfw
    * @Date: 2023/12/3
    * @Date: 14:20
    */
    @Override
    public void createStockItemToMengNiu(Long  bosId) {
        List<CreateItemModel> list = new ArrayList<>();
        //查询商品
        String token = restTemplateUtil.getToken(bosId);
        //首先获取商品列表总数
        GoodsListResponse goodsListResponse = weiMengGoodsRequest.getGoodsList(1,1,token);
        //商品总数
        int totalCount = goodsListResponse.getData().getTotalCount();
        int pageSize = 20;
        int pageNum ;
        int pageTotal = (int) Math.ceil(totalCount * 1.0 / pageSize);
        for(int i=1;i<=pageTotal;i++){
            pageNum = i ;
            //调用蒙牛接口查询商品
            GoodsListResponse goodsList = weiMengGoodsRequest.getGoodsList(pageNum, pageSize,token);
            //循环商品
            List<GoodsListResponse.PageList> pageList = goodsList.getData().getPageList();
            for (GoodsListResponse.PageList pl:pageList){
                CreateItemModel createItemModel = new CreateItemModel();
                //通过id查询商品详情进行参数封装
                GoodsInfoModel goodsInfo = weiMengGoodsRequest.getGoodsInfoById(pl.getGoodsId(), token);
                list.add(this.convert(goodsInfo,createItemModel));
            }
        }
        remoteItemService.createItem(list, "youzan-collect");
    }
    /**
     * 将微盟数据转换为蒙牛需要的数据
     * @param goodsInfo
     * @return
     */
    private CreateItemModel convert(GoodsInfoModel goodsInfo, CreateItemModel createItemModel) {
        fillBasicItemInfo(goodsInfo, createItemModel);
        createItemModel.setCreatorId("蒙牛中间件");
        createItemModel.setSource("wm");
        createItemModel.setItemId(goodsInfo.getData().getGoodsId() + "");
        return createItemModel;
    }

    /***
     * 修改商品
     * @param goodsInfo
     * @param modifyItemModel
     * @return
     */
    public ModifyItemModel convertUpdate(GoodsInfoModel goodsInfo, ModifyItemModel modifyItemModel) {
        fillBasicItemInfo(goodsInfo, modifyItemModel);
        modifyItemModel.setModifier("蒙牛中间件");
        modifyItemModel.setItemId(goodsInfo.getData().getGoodsId() + "");
        return modifyItemModel;
    }


    /**
     * 封装基本参数
     * @param goodsInfo
     * @param baseMnItemModel
     */
    private void fillBasicItemInfo(GoodsInfoModel goodsInfo, BaseMnItemModel baseMnItemModel) {
        baseMnItemModel.setFromSys("DC");
        // 对各个属性进行赋值
        baseMnItemModel.setLeafCategoryId("567097729763542667");
//        baseMnItemModel.setLeafCategoryId(goodsInfo.getData().getCategoryList().get(0).getCategoryId()+"");  // 商品类目
        baseMnItemModel.setItemTitle("测试商品同步");
        //baseMnItemModel.setItemTitle(goodsInfo.getData().getTitle());  // 商品标题
        baseMnItemModel.setSubdescription(goodsInfo.getData().getSubTitle());  // 商品副标题
        baseMnItemModel.setMemo(goodsInfo.getData().getGoodsDesc());  // 商品简介
        baseMnItemModel.setItemType(goodsInfo.getData().getGoodsType()+"");  // 商品类型
        baseMnItemModel.setSalePoints1("");  // 商品卖点1
        baseMnItemModel.setSalePoints2("");  // 商品卖点2
        baseMnItemModel.setSalePoints3("");  // 商品卖点3

        // 商品图片信息
        List<BaseMnItemModel.Image> imageList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(goodsInfo.getData().getGoodsImageUrl())){
            int total = goodsInfo.getData().getGoodsImageUrl().size()>4 ? 4:goodsInfo.getData().getGoodsImageUrl().size();
            for (int i =0;i<total ;i++){
                BaseMnItemModel.Image image1 = new BaseMnItemModel.Image();
                image1.setUrl(goodsInfo.getData().getGoodsImageUrl().get(i));
                image1.setPosition(i+1);
                if (i==0){
                    image1.setMajor("true");
                }else{
                    image1.setMajor("false");
                }
                imageList.add(image1);
            }
        }else{
            BaseMnItemModel.Image image1 = new BaseMnItemModel.Image();
            image1.setUrl(goodsInfo.getData().getDefaultImageUrl());
            image1.setPosition(1);
            image1.setMajor("true");
            imageList.add(image1);
        }
        baseMnItemModel.setImageList(imageList);

        // 视频列表
        List<BaseMnItemModel.Video> videoList = new ArrayList<>();
        BaseMnItemModel.Video video1 = new BaseMnItemModel.Video();
        if (!StringUtils.isEmpty(goodsInfo.getData().getGoodsVideoUrl())){
            video1.setUrl(goodsInfo.getData().getGoodsVideoUrl());
            video1.setPosition(1);
            video1.setMajor("true");
            videoList.add(video1);
            baseMnItemModel.setVideoList(videoList);
        }

        baseMnItemModel.setDescription(goodsInfo.getData().getGoodsDesc());  // 商品文描
        // 后台类目描述属性
        if(!CollectionUtils.isEmpty(goodsInfo.getData().getGoodsPropertyList())){
            List<BaseMnItemModel.ItemPropertyValue> itemPropertyValueList = new ArrayList<>();
            for (GoodsInfoModel.GoodsProperty g:goodsInfo.getData().getGoodsPropertyList()) {
                BaseMnItemModel.ItemPropertyValue propertyValue1 = new BaseMnItemModel.ItemPropertyValue();
                propertyValue1.setPropertyId(g.getPropId()+"");
                propertyValue1.setPropertyText(g.getPropValueName());
                propertyValue1.setValueId(g.getPropValueId()+"");
                propertyValue1.setValueText(g.getPropValueName());
                itemPropertyValueList.add(propertyValue1);

            }
            baseMnItemModel.setItemPropertyValueList(itemPropertyValueList);
        }else {
            baseMnItemModel.setItemPropertyValueList(null);
        }


        List<BaseMnItemModel.ItemPropertyValue> salePropertyValueList2 = new ArrayList<>();
        BaseMnItemModel.ItemPropertyValue saleProperty2 = new BaseMnItemModel.ItemPropertyValue();
        saleProperty2.setPropertyId("574417247198066226");
        saleProperty2.setPropertyText("顺丰冷链");
        saleProperty2.setValueId("574417393822545368");
        saleProperty2.setValueText("顺丰冷链");
        salePropertyValueList2.add(saleProperty2);



        baseMnItemModel.setSelfSupportType("1");  // 自营类型
        baseMnItemModel.setAllowBooking("1");  // 是否可以预定
        baseMnItemModel.setSaleRange("1");  // 可用范围
        baseMnItemModel.setPayType("7");  // 支付方式
        // 支持履约方式
        List<String> supportDeliveryType = new ArrayList<>();
        supportDeliveryType.add("1");
        supportDeliveryType.add("2");
        supportDeliveryType.add("3");
        baseMnItemModel.setSupportDeliveryType(supportDeliveryType);
        // 选购方式
        List<String> saleRule = new ArrayList<>();
        saleRule.add("1");
        saleRule.add("2");
        baseMnItemModel.setSaleRule(saleRule);

        baseMnItemModel.setAllowSearch("1");  // 是否可被搜索展示
        baseMnItemModel.setNewExclusive("1");  // 新客专享
        baseMnItemModel.setAllowAfterSale("1");  // 是否允许售后
        baseMnItemModel.setReturnPolicy("2");  // 是否支持7天无理由退货

        // 店铺配置
        List<BaseMnItemModel.StoreConfig> storeConfig = new ArrayList<>();
        BaseMnItemModel.StoreConfig config1 = new BaseMnItemModel.StoreConfig();
        config1.setStoreCode("1000061124");
        config1.setInitInvQuantity(100);
        config1.setMinimumDiscount("10");
        storeConfig.add(config1);
        baseMnItemModel.setStoreConfig(storeConfig);

        // 商品规格sku
        List<BaseMnItemModel.Sku> skuList = new ArrayList<>();

        List<GoodsInfoModel.Sku> skuList1 = goodsInfo.getData().getSkuList();


        if (!CollectionUtils.isEmpty(skuList1)){
            for (GoodsInfoModel.Sku sku: skuList1) {
                BaseMnItemModel.Sku sku1 = new BaseMnItemModel.Sku();
                sku1.setProductId("574371959438237855");
                sku1.setPrice( (int) Math.round(sku.getSalePrice()));

               /* // sku图片
                List<BaseMnItemModel.Image> skuImageList = new ArrayList<>();
                BaseMnItemModel.Image skuImage1 = new BaseMnItemModel.Image();
                skuImage1.setUrl("http://example.com/skuimage1.jpg");
                skuImage1.setPosition(1);
                skuImage1.setMajor("true");
                BaseMnItemModel.Image skuImage2 = new BaseMnItemModel.Image();
                skuImage2.setUrl("http://example.com/skuimage2.jpg");
                skuImage2.setPosition(2);
                skuImage2.setMajor("false");
                skuImageList.add(skuImage1);
                skuImageList.add(skuImage2);*/

                sku1.setImageList(null);
                // sku销售属性
                List<BaseMnItemModel.ItemPropertyValue> salePropertyValueList = new ArrayList<>();
                if (!CollectionUtils.isEmpty(sku.getSkuSpecValueList())){
                    List<GoodsInfoModel.SkuSpecValue> skuSpecValueList = sku.getSkuSpecValueList();
                    for (GoodsInfoModel.SkuSpecValue skuSpecValue:skuSpecValueList) {
                        BaseMnItemModel.ItemPropertyValue saleProperty1 = new BaseMnItemModel.ItemPropertyValue();
                        saleProperty1.setPropertyId(skuSpecValue.getSpecId()+"");
                        saleProperty1.setPropertyText(skuSpecValue.getSpecName());
                        saleProperty1.setValueId(skuSpecValue.getSpecValueId()+"");
                        saleProperty1.setValueText(skuSpecValue.getSpecValueName());
                        salePropertyValueList.add(saleProperty1);
                    }
                    sku1.setSalePropertyValueList(salePropertyValueList);
                }else {
                    sku1.setSalePropertyValueList(salePropertyValueList2);
                }

                // sku配置
                BaseMnItemModel.Sku.SkuConfig skuConfig = new BaseMnItemModel.Sku.SkuConfig();
                skuConfig.setOrderSaleCount("0");
                skuConfig.setWeight(sku.getWeight()+"");
                skuConfig.setVolume(sku.getVolume()+"");
                skuConfig.setMinRefPrice(sku.getCostPrice()+"");
                skuConfig.setCostPrice(sku.getCostPrice()+"");
                skuConfig.setTagPrice(sku.getCostPrice()+"");
                skuConfig.setStaffPrice(sku.getMarketPrice()+"");
                skuConfig.setRedLinePrice(sku.getCostPrice()+"");
                skuConfig.setMainSku(1+"");
                sku1.setSkuConfig(skuConfig);
                skuList.add(sku1);
                baseMnItemModel.setSkuList(skuList);
            }
        }else{
            baseMnItemModel.setSkuList(null);
        }

    }

}
